package com.mall4jcola.cloud.handler;

import cn.dev33.satoken.exception.NotLoginException;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.exception.BizException;
import com.mall4jcola.cloud.constant.HttpEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 *
 * @author ruoyi
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 自定义异常
     */
    @ExceptionHandler(BizException.class)
    public Response baseException(BizException e) {
        log.error(e.getMessage(),e);
        return Response.buildFailure(HttpEnum.Biz.BIZ_FAIL.getCode(),e.getMessage());
    }
    @ExceptionHandler(NotLoginException.class)
    public Response notLoginException(NotLoginException e){
        log.error(e.getMessage(),e);
        return Response.buildFailure(HttpEnum.LoginRegisterBiz.UNAUTHORIZED.getCode(),
                HttpEnum.LoginRegisterBiz.UNAUTHORIZED.getMsg());
    }
    @ExceptionHandler(Exception.class)
    public Response exception(Exception e) {
        log.error(e.getMessage(),e);
        return Response.buildFailure(HttpEnum.Sys.SYS_FAIL.getCode(),e.getMessage());
    }

}
